package com.logic.saas.pojo;

import java.io.Serializable;
import java.lang.reflect.Field;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;

import com.logic.saas.util.annotation.Column;
import com.logic.saas.util.annotation.Table;
import com.logic.saas.util.annotation.UniqueConstraint;

public class BasePojo implements Serializable{

	private static final long serialVersionUID = 7928271657902930457L;
	protected Timestamp latestModifiedDate;
	protected Integer latestModifiedBy;
	protected Timestamp createdDate;
	protected Integer createdBy;
	@Column(name = "id")
	protected Integer id;
	
	protected String displayText;
	protected final int displayTextLength=55;
	
	public Timestamp getLatestModifiedDate() {
		return latestModifiedDate;
	}
	public void setLatestModifiedDate(Timestamp latestModifiedDate) {
		this.latestModifiedDate = latestModifiedDate;
	}
	public Integer getLatestModifiedBy() {
		return latestModifiedBy;
	}
	public void setLatestModifiedBy(Integer latestModifiedBy) {
		this.latestModifiedBy = latestModifiedBy;
	}
	public Timestamp getCreatedDate() {
		return createdDate;
	}
	public void setCreatedDate(Timestamp createdDate) {
		this.createdDate = createdDate;
	}
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public Integer getCreatedBy() {
		return createdBy;
	}
	public void setCreatedBy(Integer createdBy) {
		this.createdBy = createdBy;
	}
	public String getDisplayText() {
		return displayText;
	}
	public void setDisplayText(String displayText) {
		this.displayText = displayText;
	}
	/** 
     * 获取POJO的table名 
     * 需要定义@Id 
     * @return String
     * @author jeffdong
     */ 
	public String tablename() {  
        Table table = this.getClass().getAnnotation(Table.class);  
        if(table != null)  
            return table.name();  
        else  
            throw new RuntimeException("undefine POJO @Table, need Tablename(@Table)");  
    }  
	
	/**
	 * 得到所有的唯一索引的字段
	 * @return List<List<Field>>  - index的list， 每一项都是一个unique的约束（Map<数据库字段名,pojo字段名>）
	 * @author jeffdong
	 */
	public List<List<Field>> uniqueKeys() { 
		List<List<Field>> keysList = new ArrayList<List<Field>>();
		
        Table table = this.getClass().getAnnotation(Table.class);  
        Map<String, Field> columnFieldMap = this.columnFieldMap();
        if(table != null && columnFieldMap != null) {
        	UniqueConstraint constraints[] = table.uniqueConstraints();
        	for(UniqueConstraint uni : constraints){
        		if(uni !=null && uni.columnNames() != null){
        			String columnNames[] = uni.columnNames();
        			List<Field> uniqueIndexList = new ArrayList<Field>();
        			for(String columnName : columnNames){
        				if(StringUtils.isNotBlank(columnName)){
        					Field field = columnFieldMap.get(columnName);
        					if(field != null){
        						uniqueIndexList.add(field);
        					}
        				}
        			}
        			if(uniqueIndexList.size() >0){
        				keysList.add(uniqueIndexList);
        			}
        		}
        	}
        } else{
        	//"undefine POJO @Table, need Tablename(@Table)" 
        }
            
        
        return keysList;
    }
	/**
	 * 得到数据库字段与pojofield名的对应关系
	 * @return
	 * @author jeffdong
	 */
	public Map<String, Field> columnFieldMap(){
		Map<String, Field> resultMap = null;
		
		Field fields[] = this.getClass().getDeclaredFields();
		if(fields != null && fields.length >0){
			resultMap = new HashMap<String, Field>();
			for(Field field: fields){
				if(field != null && field.isAnnotationPresent(Column.class)){
					Column column = field.getAnnotation(Column.class);
					if(column != null && StringUtils.isNotBlank(column.name())){
						resultMap.put(column.name(),field);
					}
				}
			}
		}
		
		return resultMap;
	}
	
	
}
